package com.techsoul.module.basic.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.id.IdWorker;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.basic.mapper.CustomerMapper;
import com.techsoul.module.basic.mapper.SupplierMapper;
import com.techsoul.module.basic.pojo.entity.Customer;
import com.techsoul.module.basic.pojo.entity.Supplier;
import com.techsoul.module.basic.pojo.param.StoreSaveParam;
import com.techsoul.module.basic.pojo.param.StoreSearchParam;
import com.techsoul.module.basic.pojo.vo.CustomerVo;
import com.techsoul.module.basic.pojo.vo.SupplierVo;
import com.techsoul.module.basic.service.IStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 店铺 Controller
 *
 * @author liuen
 * @date 2021-05-02
 */
@Api(tags = "店铺")
@RestController
@RequestMapping("/basic/store")
public class StoreController {

    @Autowired
    private Result result;

    @Autowired
    private IStoreService service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private IdWorker idWorker;
    @ApiOperation("新增")
    @Log(title = "买家各户", operationType = OperationTypeEnum.INSERT)
    @PreAuthorize("hasAnyAuthority('basic:store:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody StoreSaveParam params) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        String addStoreId= this.service.insert(params,user);
        if(user.getCustomerId()!=null&&user.getCustomerId()!=""){
            CustomerVo customer = this.customerMapper.findById(user.getCustomerId());
            Supplier supplier = new Supplier();
            supplier.setSite(addStoreId);
            supplier.setSupplierCode(customer.getCustomerCode());
            supplier.setSupplierDesc(customer.getCustomerDesc());
            supplier.setAddress(customer.getAddress());
            supplier.setBizLicenceNo(customer.getBizLicenceNo());
            supplier.setSupplierName(customer.getCustomerName());
            supplier.setSupplierType("Trade");
            supplier.setContactName(customer.getContactName());
            supplier.setContactPhone(customer.getContactPhone());

            supplier.setSupplierFullName(customer.getCustomerFullName());
            supplier.setId(user.getCustomerId() + "").preInsert(user.getId());
            this.supplierMapper.insert(supplier);
        }



        return result.success(addStoreId);
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
    @Log(title = "店铺", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:store:delete')")
    @PostMapping("/logicalDelete/{ids}")
    public Result logicalDelete(@PathVariable String[] ids) {
        return result.success(this.service.logicalDelete(Arrays.asList(ids)));
    }

    @ApiOperation("批量作废（多个用逗号隔开）")
    @Log(title = "店铺", operationType = OperationTypeEnum.DELETE)
    @PreAuthorize("hasAnyAuthority('basic:store:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @Log(title = "店铺", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:store:update')")
    @PutMapping
    public Result update(@Valid @RequestBody StoreSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('basic:store:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
    @PreAuthorize("hasAnyAuthority('basic:store:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody StoreSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
    @PreAuthorize("hasAnyAuthority('basic:store:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody StoreSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("校验店铺编码")
    @Log(title = "校验店铺编码", operationType = OperationTypeEnum.UPDATE)
    @PreAuthorize("hasAnyAuthority('basic:store:search')")
    @PostMapping("/checkStoreCode")
    public Result checkStoreCode(@Valid @RequestBody StoreSearchParam params) {
        return result.success(this.service.checkStoreCode(params));
    }

}
